<template>
  <div class="pie-chart" :style="{width: width, height: height}"></div>
</template>
<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons')
  export default {
    name: 'PieChart',
    props: {
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '333px'
      }
    },
    data () {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    methods: {
      initChart(){
        this.chart = echarts.init(this.$el)

        let option = {
          tooltip: {
            trigger: 'item'
          },
          legend: {
            top: '5%',
            left: 'center',
            itemWidth: 10, // 修改图例的图标
            itemHeight: 10,
            textStyle: { // 图例(文字说明)样式
              color: '#fff'
            }
          },
          series: [
            {
              name: 'one',
              type: 'pie',
              radius: ['25%', '50%'],
              avoidLabelOverlap: true,
              roseType: 'area',
              label: {
                // show: true,
                formatter: '{c|{c}}\n{b|{b}}',
                rich: {
                  c: {
                    color: '#dee09f'
                  },
                  b: {
                    color: '#3772aa'
                  },
                }
              },
              emphasis: {
                label: {
                  show: true,
                  fontSize: 16,
                  fontWeight: 'bold'
                }
              },
              labelLine: {
                show: true,
                length: 10
              },
              data: [
                { value: 1048, name: '服装', itemStyle: { color: '#0184E1'} },
                { value: 735, name: '娱乐', itemStyle: { color: '#ffdb5e'} },
                { value: 580, name: '食品', itemStyle: { color: '#2fc6ed'} },
                { value: 484, name: '海鲜', itemStyle: { color: '#9de7ba'} },
              ]
            }
          ]
        };
        this.chart.setOption(option)
      }
    }
  }
</script>